package Servlets.lhl;/**
 * 项目名称：SaleOderManger.java
 * 文件名称：SaleOderManger.java
 * 类的描述：无
 * 创建时间：2025-07-24 18:51:11
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.List;

import Bean.sales_order;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "SaleOderManger", value = "/salerorder")
public class SaleOderManger extends HttpServlet {
    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        Integer page = 1;
        if(req.getParameter("page")!= null){
            page = Integer.parseInt(req.getParameter("page"));
        }
        if(page < 1 || page==null){
            page = 1;
        }
        Integer pageSize = 10;
        if(req.getSession(false).getAttribute("count") == null){
            int i = GokJDBCUtil2.queryForCount("select count(*) from sales_order", null);
            int count = i%pageSize==0?i/pageSize:i/pageSize+1;
            req.getSession().setAttribute("count", count);
        }
        List<sales_order> salesOrders = GokJDBCUtil2.queryAll("select id,code,userid,ordertime,totalamount,paymentstatus,orderstatus,remark,isdeleted,(select username from user where userid =user.id) as username from sales_order where isdeleted = 0 order by id desc limit ?,?", sales_order.class, (page-1)*pageSize, pageSize);
        req.setAttribute("salesOrders", salesOrders);
        req.getRequestDispatcher("pages/sellOrder_manage.jsp").forward(req,res);
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doGet(req, res);
            }else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}